home *** CD-ROM | disk | FTP | other *** search
- /* Library for displaying the contents of the clipboard in a window.
-
- 93/12/16 aih
- - added task to update clipboard
- - clipboard window's position is saved in prefs
-
- 93/11/20 aih
- - instead of moving the clipboard offscreen on suspend events, the
- clipboard is hidden by calling ShowHide, which doesn't change window
- order
-
- 93/10/19 aih
- - made some changes...
-
- 93/03/16 AIH
- - Added flag to keep text from being activated
-
- 93/03/10 AIH
- - The window's minimum and maximum size are set to fix a problem with
- zooming the window
-
- 93/03/06 AIH
- - Added read-only flag to text
- - Menu commands are disable if memory is low
-
- 92/02/27 AIH
- - Simplified event handling
-
- 91/11/14 AIH
- - Fixed prototype mismatch
-
- 91/05/29 AIH
- - Got this to work.
-
- 91/05/15 Ari Halberstadt (AIH)
- - Created this library */
-
- #include <limits.h>
- #include <string.h>
- #include "ClipboardLib.h"
- #include "DrawLib.h"
- #include "EventLib.h"
- #include "LowMemLib.h"
- #include "MemoryLib.h"
- #include "PreferencesLib.h"
- #include "RectangleLib.h"
- #include "ResourceConstantsLib.h"
- #include "TextScrollLib.h"
- #include "WindowLib.h"
-
- static ClipboardHandle gClipboard;
-
- /* true if the clipboard is valid */
- Boolean ClipboardValid(ClipboardHandle clipboard)
- {
- return(HandleValidSize(clipboard, sizeof(ClipboardType)));
- }
-
- /* task to update clipboard */
- static void ClipboardTask(TaskHandle task, void *data)
- {
- ClipboardRefresh(data);
- }
-
- /* create a clipboard window */
- ClipboardHandle ClipboardBegin(void)
- {
- Rect newRect; /* rectangle for new window */
- Rect portRect; /* window's port rectangle */
- short scrapCount; /* for setting the scrapCount field */
- WindowPtr window = NULL; /* new window */
- TextScrollHandle scrl = NULL; /* the new scroll text */
- TaskHandle task = NULL; /* update task */
- volatile ClipboardHandle clipboard = NULL;
-
- TRY {
-
- /* create the clipboard window */
- clipboard = HandleBeginClear(sizeof(ClipboardType));
- window = WinGet(RLW_CLIPBOARD);
- (**clipboard).window = window;
-
- /* position the window in the lower left corner of the screen */
- WinPortRect(window, &portRect);
- WinNewRect(window, &newRect);
- newRect.top = newRect.bottom - RectHeight(&portRect);
- newRect.right = newRect.left + RectWidth(&portRect);
- WinMove(window, newRect.left, newRect.top);
-
- /* create the text */
- InsetRect(&portRect, -1, -1);
- scrl = TxScrlBegin(window, &portRect, true, true, false, false,
- TX_UNSTYLED_KIND, NULL);
- (**clipboard).scrl = scrl;
- TxWrapSet(TxScrlText(scrl), false);
- FrameFlagsSet(TxScrlFrame(scrl),
- FrameFlags(TxScrlFrame(scrl)) | FRAME_KEEP_INACTIVE);
- TxFlagsSet(TxScrlText(scrl),
- TxFlags(TxScrlText(scrl)) | TX_READ_ONLY | TX_IGNORE_CLICK);
-
- /* Set the scrap count to force a clipboard refresh even
- if the scrap hasn't been modified since the application started
- up. This is necessary since GetScrapCount() returns 0, even though
- there may be information in the scrap that we should display
- (0 is the same as what the clipboard's scrapCount field would
- be if we didn't explicitely initialize it here). */
- scrapCount = GetScrapCount() - 1;
- (**clipboard).scrapCount = scrapCount;
-
- /* insert task to update clipboard */
- task = EventPostTaskInsert(ClipboardTask, clipboard);
- (**clipboard).task = task;
-
- ClipboardRefresh(clipboard);
- WinRegister(window, clipboard, ClipboardEventTable());
- WinZoomRestore(window, PrefsFile(), PREFS_CLIPBOARD_POSITION);
- WinShow(window);
- } CATCH {
- ClipboardEnd(clipboard);
- } ENDTRY;
- ensure(ClipboardValid(clipboard));
- return(clipboard);
- }
-
- /* end use of the clipboard window */
- void ClipboardEnd(ClipboardHandle clipboard)
- {
- if (clipboard) {
- EventPostTaskDelete((**clipboard).task);
- TxScrlEnd((**clipboard).scrl);
- if ((**clipboard).window)
- WinZoomSave((**clipboard).window, PrefsFile(), PREFS_CLIPBOARD_POSITION);
- WinUnregister((**clipboard).window, clipboard);
- WinEnd((**clipboard).window);
- HandleEnd(clipboard);
- clipboard = NULL;
- }
- ensure(! ClipboardValid(clipboard));
- }
-
- /* refresh the data displayed in the clipboard window */
- void ClipboardRefresh(ClipboardHandle clipboard)
- {
- TextHandle text = NULL;
-
- require(! clipboard || ClipboardValid(clipboard));
- if (clipboard && (**clipboard).scrapCount != GetScrapCount()) {
- text = TxScrlText((**clipboard).scrl);
- TxSelectAll(text);
- TxDelete(text);
- TxPaste(text);
- TxSelect(text, 0, 0);
- TxScrlChanged((**clipboard).scrl);
- TxScrlShowSel((**clipboard).scrl);
- (**clipboard).scrapCount = GetScrapCount();
- }
- ensure(! clipboard || (**clipboard).scrapCount == GetScrapCount());
- }
-
- /* hide clipboard window on suspend events */
- void ClipboardSuspend(void)
- {
- if (gClipboard)
- ShowHide((**gClipboard).window, false);
- }
-
- /* show clipboard window on resume events */
- void ClipboardResume(void)
- {
- if (gClipboard)
- ShowHide((**gClipboard).window, true);
- }
-
- /* true if clipboard window has the focus */
- static Boolean ClipboardHasFocus(void)
- {
- return(gClipboard && WinHasFocus((**gClipboard).window));
- }
-
- /* adjust the menu */
- void ClipboardAdjustMenu(void)
- {
- if (ClipboardHasFocus()) {
- MenuCmdEnable(CMD_CLOSE);
- MenuCmdCheck(CMD_CLIPBOARD, true);
- }
- if (! WinModalHasFocus() && MemWarning() < MEM_WARNING_VERY_LOW) {
- MenuCmdEnable(CMD_CLIPBOARD);
- MenuCmdEnable(CMD_EDIT);
- }
- }
-
- /* show the clipboard */
- void ClipboardShow(void)
- {
- if (! gClipboard)
- gClipboard = ClipboardBegin();
- WinSelect((**gClipboard).window);
- }
-
- /* hide the clipboard */
- void ClipboardHide(void)
- {
- ClipboardEnd(gClipboard);
- gClipboard = NULL;
- }
-
- /* handle a menu command */
- Boolean ClipboardMenu(const MenuPickType *pick)
- {
- Boolean handled = false;
-
- switch (pick->cmd) {
- case CMD_CLIPBOARD:
- ClipboardShow();
- handled = true;
- break;
- case CMD_CLOSE:
- if (ClipboardHasFocus()) {
- ClipboardHide();
- handled = true;
- }
- break;
- }
- return(handled);
- }
-
- /* close the clipboard if there isn't enough memory */
- void ClipboardMemoryLow(void)
- {
- if (MemWarning() >= MEM_WARNING_VERY_LOW)
- ClipboardHide();
- }
-